<template>
  <el-container direction="vertical">
    <el-row><Header /></el-row>
    <el-row>
      <div class="main">
        <el-card class="box-card">
          <div
            slot="header"
            style="
              text-align: center;
              font-size: 30px;
              font-family: cursive;
              color: #4f7298;
            "
          >
            <span>注册界面</span>
          </div>
          <!-- 注册表单 -->
          <el-form
            :model="ruleForm"
            ref="ruleForm"
            label-width="80px"
            class="Form"
          >
            <!-- ref注册引用信息 在代码中能够对ruleForm表单组件进行引用 -->
            <el-form-item label="用户名">
              <el-input v-model="ruleForm.checkname"></el-input>
            </el-form-item>
            <br />
            <el-form-item label="昵称">
              <el-input v-model="ruleForm.nickname"></el-input>
            </el-form-item>
            <br />
            <el-form-item label="密码">
              <el-input type="password" v-model="ruleForm.pass"></el-input>
            </el-form-item>
            <br />
            <el-form-item label="确认密码">
              <el-input type="password" v-model="ruleForm.checkPass"></el-input>
            </el-form-item>
            <br />
            <el-form-item style="margin-left: 110px">
              <el-button
                type="primary"
                @click.prevent="submitForm"
                style="margin-left: 30px"
                >注册</el-button
              >
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </el-row>
    <el-row><Footer /></el-row>
  </el-container>
</template>

<script>
import Header from "../components/Header.vue";
import Footer from "../components/Footer.vue";

export default {
  components: {
    Header,
    Footer,
  },
  data() {
    return {
      ruleForm: {
        pass: "",
        checkPass: "",
        checkname: "",
        nickname: "",
      },
    };
  },
  methods: {
    submitForm() {
      if (this.ruleForm.pass != this.ruleForm.checkPass) {
        return alert("两次密码不一致！");
      }
      this.axios
        .post("/api/users/reg", { //携带uname、upass、nickname作为参数发送
          // 2.获取注册表单中的用户数据
          uname: this.ruleForm.checkname,
          upass: this.ruleForm.pass,
          nickname: this.ruleForm.nickname,
        })
        .then((res) => {
          if (res.data.code == 200) {
            this.$router.push("/login");
          } else {
            alert(res.data.message);
          }
        })
        .catch((err) => {
          alert("服务器没有提供您所需的服务");
          // console.log(err.message);
        });
    },
  },
};
</script>

<style>
.main {
  background-image: url(../assets/images/a.webp);
  background-size: 100% 100%;
  height: 700px;
}
.box-card {
  position: absolute;
  margin: 120px 660px;
  width: 500px;
  background-color: rgb(206, 203, 199);
}
.Form {
  margin-left: -20px;
  margin-top: 40px;
}
</style>
